package com.example.system.service.impl;

import com.example.common.Redis.RedisUtils;
import com.example.common.WebSocket.SendSocket;
import com.example.common.WebSocket.WebSocket;
import com.example.common.constant.RedisConstant;
import com.example.system.domain.Bo.NoticesBo;
import com.example.system.domain.Bo.PageBo;
import com.example.system.domain.Notices;
import com.example.system.domain.Vo.PageVo;
import com.example.system.mapper.NoticeMapper;
import com.example.system.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    String key = RedisConstant.getList_Notice;
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private WebSocket webSocket;

    @Override
    public List<Notices> getListIndex() {
        return ((List<Notices>) redisUtils.get(key)).subList(0, 12);
    }

    @Override
    public void createNoticeRedis() {
        List<Notices> list = noticeMapper.getList();
        long time = RedisConstant.getList_Notice_Time;
        redisUtils.set(key, list, time);
    }

    @Override
    public PageVo<Notices> getList(PageBo pageBo) {
        List<Notices> list = (List<Notices>) redisUtils.get(key);
        return new PageVo<>(pageBo, list);
    }

    @Override
    public void insert(NoticesBo bo) {
        bo.setTime(LocalDate.now());
        noticeMapper.insertBo(bo);
        SendSocket socket = new SendSocket();
        socket.setType("info");
        socket.setSocket("success", bo.getTitle());
        //发送全体通知
        webSocket.sendAllMessage(socket);
    }
}
